<?php
class InfoNavAction extends CommonAction {

    public function index() {
        $infoNav = D('InfoNav');
        parent::search(array(
            'name'=>'导航名称',
        ));
        if(isset($_GET['pid'])) {
            $pid = $this->_get('pid');
            if(!isset($_GET['act'])) {
                parent::page($infoNav->where('pid='.$pid)->count());
                $this->assign('allinfonav', $infoNav->field(array('id', 'name', 'info', 'sort', 'is_active'))->where('pid='.$pid)->order('is_active DESC,sort ASC')->limit($this->limit)->select());
            } else if($_GET['act'] == 'search') {
                $this->map['pid'] = $pid;
                parent::page($infoNav->where($this->map)->count());
                $this->assign('allinfonav', $infoNav->field(array('id', 'name', 'info', 'sort', 'is_active'))->where($this->map)->order('is_active DESC,sort ASC')->limit($this->limit)->select());
            }

            $this->assign('pinfonav', $infoNav->where('id='.$pid)->getField('name'));
        } else {
            if(!isset($_GET['act'])) {
                parent::page($infoNav->where('pid=0')->count());
                $this->assign('allinfonav', $infoNav->field(array('id', 'name', 'info', 'sort', 'is_active'))->where('pid=0')->order('is_active DESC,sort ASC')->limit($this->limit)->select());
            } else if($_GET['act'] == 'search') {
                $this->map['pid'] = 0;
                parent::page($infoNav->where($this->map)->count());
                $this->assign('allinfonav', $infoNav->field(array('id', 'name', 'info', 'sort', 'is_active'))->where($this->map)->order('is_active DESC,sort ASC')->limit($this->limit)->select());
            }

        }
        $this->assign('empty', '<tr><td colspan="7">没有任何数据</td></tr>');
        $this->display();
    }

    public function add() {
        $infoNav = D('InfoNav');
        $infoAttr = D('InfoAttr');
        //判断是否子类导航的增加
        if(isset($_GET['pid'])) {
            $pid = $this->_get('pid');
            $this->assign('pinfonav', $infoNav->where('id='.$pid)->getField('name'));
        }
        if(isset($_POST['send'])) {
            if($infoNav->create()) {
                if(!isset($_GET['pid'])) {
                    $infoNav->add() ? $this->success('新增导航成功!', __URL__) : $this->error('系统出错，新增导航失败!', 0);
                } else {
                    $infoNav->add() ? $this->success('新增导航成功!', __URL__.'/index/pid/'.$_GET['pid']) : $this->error('系统出错，新增导航失败!', 0);
                }
            } else {
                $this->error($infoNav->getError());
            }
        }
        $attr = $infoAttr->field(array('id', 'name'))->select();
        $this->assign('attr', $attr);
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $infoNav = D('InfoNav');
            $infoAttr = D('InfoAttr');
            if(!$infoNav->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $oneNav = $infoNav->field(array('id', 'name', 'info', 'sort', 'is_active', 'attr'))->where('id='.$id)->find();
            //关联属性转换为数组
            $oneNav['attr'] = explode(',', $oneNav['attr']);

            if(isset($_POST['send'])) {
                if($infoNav->create()) {
                    if(!isset($_POST['hpid'])) {
                        $infoNav->save() ? $this->success('修改导航成功!', __URL__) : $this->error('系统出错，修改导航失败!', 0);
                    } else {
                        $infoNav->save() ? $this->success('修改导航成功!', __URL__.'/index/pid/'.$_POST['hpid']) : $this->error('系统出错，修改导航失败!', 0);
                    }
                } else {
                    $this->error($infoNav->getError());
                }
            }
            $pid = $infoNav->where('id='.$id)->getField('pid');
            $attr = $infoAttr->field(array('id', 'name'))->select();
            $this->assign(array(
                'pid'=>$pid,
                'infonav'=>$oneNav,
                'attr'=>$attr
            ));
            $this->display();
        }
    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $infoNav = D('InfoNav');
            if(!$infoNav->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $pid = $infoNav->where('id='.$id)->getField('pid');
            if($pid == 0) {
                $infoNav->where("id=$id AND pid=$id")->delete() ? $this->success('删除导航成功!', __URL__) : $this->error('系统出错，删除导航失败!', 0);
            } else {
                $infoNav->where('id='.$id)->delete() ? $this->success('删除导航成功!', __URL__.'/index/pid/'.$pid) : $this->error('系统出错，删除导航失败!', 0);
            }
        }
    }

    public function mulDelete() {
        $infoNav = D('InfoNav');
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $map['id'] = array('in', $ids);
        $pid = $infoNav->where($map)->getField('pid');
        if($pid == 0) {
            $map['pid'] = array('in', $ids);
            $map['_logic'] = 'or';
            $infoNav->where($map)->delete() ? $this->success('批量导航成功!', __URL__) : $this->error('系统出错，批量导航失败!', 0);
        } else {
            $infoNav->where($map)->delete() ? $this->success('批量导航成功!', __URL__.'/index/pid/'.$pid) : $this->error('系统出错，批量导航失败!', 0);
        }
    }

    //排序
    public function sort() {
        $infoNav = D('InfoNav');
        $idArr = $_POST['sid'];
        foreach ($_POST['sort'] as $key=>$value) {
            $data['id'] = $idArr[$key];
            $data['sort'] = $value;
            $infoNav->save($data);
        }
        $this->success('排序成功!', __URL__);
    }


}

?>